package com.ruijie.autotest.log.validation.hangzhouxiruan;

import com.minelx.landisk.filesystem.Paths;
import com.minelx.landisk.filesystem.core.Directory;
import com.minelx.landisk.filesystem.core.File;
import com.ruijie.autotest.log.common.crypto.*;
import com.ruijie.autotest.log.core.data.ITable;
import com.ruijie.autotest.log.impl.parse.*;
import com.ruijie.autotest.log.validation.hangzhoudewangxin.WIFILOGOUT.XmlAndBCPToTable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;

import static com.minelx.landisk.filesystem.core.File.byExtension;

@SpringBootApplication
public class HangZhouXiRuanTest {

	private static final String PASSWORD = "12345678";

	private static final String IV_PASSWORD = "12345678";

	public static void main(String[] args) throws Exception {
		ConfigurableApplicationContext context = SpringApplication.run(HangZhouXiRuanTest.class, args);

		ICryptoFactory cryptoFactory =
				Base64CryptoFactory.get().before(CBC.create(new DESAlgorithm(), PASSWORD, IV_PASSWORD, Padding.PKCS_5_PADDING).cryptoFactory());
		List<File> files = ICryptoFactory.decryptedFiles(rawFiles(), cryptoFactory.decryption());

		List<ITable> tables = fileToTable().mapToTables(files);

		CompositeColumnVerification verified = new CompositeColumnVerification(context, tables);
		verified.verify();
		System.out.println("we done.");
	}

	private static XmlAndBCPToTable fileToTable() {
		return new XmlAndBCPToTable(new ColumnsAndValuesParser(
				new XmlColumnsParser(),
				new LinesParser("\r\n|\n"),
				new SplitCellsParser("\t")
		));
	}

	private static List<File> rawFiles() {
		Directory rootDirectory = Paths.root("/D:/FTP/杭州西软");
		return rootDirectory.childrenFiles()
				.filter(which -> !which.getName().startsWith("decrypted_")) // not starts with "decrypted_"
				.filter(byExtension("zip")).all();
	}
}
